//
//  receiveFirCirGetBgImage.m
//  tcpoctest
//
//  Created by gump on 5/8/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "receiveFirCirGetBgImage.h"
#import "LocalPathDataSource.h"

#import "FirCirBgImageDataSource.h"
#import "FirCirBgImagedb.h"

@implementation receiveFirCirGetBgImage

-(void)processreceive
{
    //返回 74char(1),24char(1) 用户名长度char(1),
    //用户名char(x),返回值char(1)、1有背景图2无背景图,
    //url长度char(1),URLchar(x)
    
    NSString *mail = [logicsb ReadUnencodeString];
    char bhave = [logicsb ReadChar];
    
    
    
    if(bhave == 1)
    {
        NSString *url = [logicsb ReadUnencodeString];
        
       
            NSString *strname = getFileNameWithDate(FT_FCBGI);
        strname = [NSString stringWithFormat:@"%@%@%d",strname,mail,[LogicControl sharedLogicControl].CurSequence];
        
            NSString * urlpath =[[[DataControl shareControl].localpathds FircirPath]
                                 stringByAppendingPathComponent:strname];
        
            
            NSData  *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
            
            if ([imagedata length] == 0) {
                return;
            }
            
            BOOL bsuccess = [imagedata writeToFile:urlpath atomically:YES];
            
            do {
                
                if (!bsuccess) {
                    break;
                }
                
                NSString *oldpath = [[DataControl shareControl].fcBgImageDS
                                     getPathByMail:mail];
                
                
                [[FileManager shareControl] deleteFile:oldpath];
                
                [[DataControl shareControl].fcBgImageDS
                 RemovePathByMail:mail];
                [[DataBaseCtrol shareControl].fircirbgimagedatabase
                 deleteBgImageByMail:mail];
                
                [[DataControl shareControl].fcBgImageDS AddImagePath:urlpath withMail:mail];
                [[DataBaseCtrol shareControl].fircirbgimagedatabase insertBgImageUrl:mail withURL:urlpath];
                
            } while (false);
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_REFRESHCURVIEW];
    }
    
    [[DataControl shareControl].fcBgImageDS delSearchOjByMail:mail];
    
    [self ShowLog];
}

@end
